package bank;

import cash_access.OverdraftException;

/**
 * Klasse fuer ein Konto
 */

public class Account extends cash_access.Account{
	private String ID; // Konto-ID
	private double balance;  // Kontostand

	public Account(String id) {
		// Initialisieren
		ID = id;
		balance = 0.0;          
	}

	/**
	 * Liefert die ID dieses Kontos.
	 * (Wird fuer die GUI benutzt.)
	 * 
	 * @return Konto-ID
	 */
	protected String getID() {
		return ID;
	}

	public double getBalance() {
		double balance;
		synchronized (this) {
			balance = this.balance;
		}
		System.out.println("Konto <"+ID+"> --> getBalance(): "+balance);
		return balance;
	}

	@Override
	public void deposit(double amount) {
		synchronized (this) {
			this.balance = this.balance + amount;
		}
		System.out.println("Konto <"+ID+"> --> deposit("+amount+"): Kontostand_neu = "+balance);
	}

	@Override
	public void withdraw(double amount) throws OverdraftException {
		synchronized (this) {
			if(amount > balance){
				//System.out.println("Konto <"+ID+"> --> withdraw("+amount+"): nicht erlaubt, Kontostand: "+balance);
				throw new OverdraftException(amount + " Geldeinheiten nicht mehr verf�gbar");
			}else if(amount < 0){
				throw new RuntimeException("Es kann nichts negatives abgehoben werden.");
			}else{
				try {
					Thread.currentThread().sleep(8000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.balance = this.balance - amount;  
				System.out.println("Konto <"+ID+"> --> withdraw("+amount+"): OK, Kontostand_neu = "+balance);          
			}
		}    
	}

}
